#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main()
{
    pid_t pid = fork();
    if (pid == 0)
    {
        sleep(2);
        printf("child pid = %d, ppid = %d\n", getpid(), getppid());
    }
    else if (pid > 0)
    {
        printf("parent\n");
        printf("parent pid = %d, ppid = %d\n", getpid(), getppid());
        int status;
        pid_t wpid = wait(&status);
        // 判断是否正常退出
        if (WIFEXITED(status))
        {
            printf("exit value: %d\n", WEXITSTATUS(status));
        }
        // 是否被信号杀死
        if (WIFSIGNALED(status))
        {
            printf("exit by signal: %d\n", WTERMSIG(status));
        }
        printf("died child pid=%d\n", wpid);
    }
    return 0;
}
